package com.xinfan.tank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;

/**
 * @Author xin
 * @Date 2022/5/31 15:47
 * @Version 1.0
 */
public class TankPanel extends JPanel implements KeyListener, Runnable {

    private MyTank mt = null;
    private Vector<EnemyTank> enemies = new Vector<>();
    private Vector<Node> nodes = new Vector<>();

    Vector<Bomb> bombs = new Vector<>();
    private int enemyCount = 5;


    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    //private com.xinfan.tank.Bullet bullet = null;
    public TankPanel(String key) {
        File file = new File(Recorder.getPath());
        if (file.exists()&&"2".equals(key)){
            nodes = Recorder.getNodes();
        }else {
            key = "1";
        }

        mt = new MyTank(300, 300);
        mt.setSpeed(2);
        //enemies = new Vector<>();
        switch (key) {
            case "1":
                for (int i = 0; i < enemyCount; i++) {
                    EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 0);

                    enemyTank.setEnemyTanks(enemies);
                    enemyTank.setDirection(2);
                    new Thread(enemyTank).start();
                    Bullet bullet = new Bullet(enemyTank.getX() + 20, enemyTank.getY() + 60, 2);
                    enemyTank.bullets.add(bullet);
                    new Thread(bullet).start();
                    enemies.add(enemyTank);


                }
                break;
            case "2":

                for (int i = 0; i < nodes.size(); i++) {
                    EnemyTank enemyTank = new EnemyTank(nodes.get(i).getX(),nodes.get(i).getY());

                    enemyTank.setEnemyTanks(enemies);
                    enemyTank.setDirection(nodes.get(i).getDirection());
                    new Thread(enemyTank).start();
                    Bullet bullet = new Bullet(enemyTank.getX() + 20, enemyTank.getY() + 60, 2);
                    enemyTank.bullets.add(bullet);
                    new Thread(bullet).start();
                    enemies.add(enemyTank);


                }
                break;
        }

        Recorder.setEnemyTanks(enemies);

        //String name = Panel.class.getName();
        //System.out.println(name);
        image1 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/1.png"));
        image2 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/11111.png"));
        image3 = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/c.jpg"));

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        if (mt != null && mt.isAlive) {
            drawTank(mt.getX(), mt.getY(), g, mt.getDirection(), 1);
        }
        drawRecord(g);

        //drawBullet(mt.getBullet(),g);
        for (int i = 0; i < enemies.size(); i++) {
            EnemyTank enemy = enemies.get(i);

            if (enemy.isAlive) {
                drawTank(enemy.getX(), enemy.getY(), g, enemy.getDirection(), 0);
                //drawBullet(enemy.getBullet(),g);

            /*for (com.xinfan.tank.Bullet bullet : enemy.bullets) {
                if (bullet.isAlive) {
                    g.draw3DRect(bullet.x, bullet.y, 2, 2, false);
                } else {
                    enemy.bullets.remove(bullet);
                }
            }*/

                for (int j = 0; j < enemy.bullets.size(); j++) {
                    Bullet bullet = enemy.bullets.get(j);
                    if (bullet.isAlive) {
                        g.draw3DRect(bullet.x, bullet.y, 2, 2, false);
                    } else {
                        enemy.bullets.remove(bullet);
                    }
                }
            } /*else {
                enemies.remove(enemy);
            }*/



        }
        //drawTank(mt.getX()+100,mt.getY(),g,3,0);

        //炸弹集合不为空画💣
        for (int i = 0; i < bombs.size(); i++) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Bomb bomb = bombs.get(i);
            if (bomb.life > 6) {
                g.drawImage(image1,bomb.x,bomb.y,72,72,this);
            } else if (bomb.life > 3) {
                g.drawImage(image2, bomb.x, bomb.y, 72, 72, this);
            } else {
                g.drawImage(image3,bomb.x,bomb.y,72,72,this);
            }
            bomb.lifeDown();
            if (bomb.life == 0) {
                bombs.remove(bomb);
            }
        }



        //画我方子弹
        /*if (mt.getBullet() != null && mt.getBullet().isAlive) {
            System.out.println("绘制子弹");
            g.draw3DRect(mt.getBullet().x, mt.getBullet().y, 2, 2, false);
        }*/
        if (mt.bullets != null) {
            for (int i = 0; i < mt.bullets.size(); i++) {
                if (mt.bullets.get(i).isAlive) {
                    g.draw3DRect(mt.bullets.get(i).x, mt.bullets.get(i).y, 2, 2, false);
                } else {
                    mt.bullets.remove(mt.bullets.get(i));
                }
            }
        }

        //绘制敌人的子弹


    }

    /*public void drawBullet(com.xinfan.tank.Bullet bullet,Graphics g) {
        g.fillOval(bullet.getX(),bullet.getY(),10,10);

    }*/

    public void drawRecord(Graphics g) {
        g.setColor(Color.BLACK);
        g.setFont(new Font("宋体",Font.BOLD,30));
        g.drawString(Recorder.getCount()+"",1080,100);
        g.drawString("你击毁的坦克数量",1020,30);
        drawTank(1020,60,g,0,0);

    }

    /**
     * @param y         起始纵坐标
     * @param x         启示横坐标
     * @param g         画笔
     * @param direction 坦克方向 0 向上 1-向有 2-向下 3-向左
     * @param type      0-敌方坦克 1-我方坦克
     */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {

        //根据type设置坦克的颜色
        switch (type) {
            case 0:
                g.setColor(Color.CYAN);
                break;
            case 1:
                g.setColor(Color.YELLOW);
                break;
        }


        switch (direction) {
            case 0:
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 1:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;
            case 2:
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 20, x, y + 20);
                //g.fillOval(bullet.getX(),bullet.getY(),10,10);
                break;
        }


    }

    public void hitMyTank() {
        if (enemies != null) {
            for (int i = 0; i < enemies.size(); i++) {
                EnemyTank enemyTank = enemies.get(i);
                for (int i1 = 0; i1 < enemyTank.bullets.size(); i1++) {
                    Bullet bullet = enemyTank.bullets.get(i1);
                    hitTank(bullet,mt);
                }
            }
        }

    }

    public void hitTank(Bullet bullet, Tank enemyTank) {
        switch (enemyTank.getDirection()) {
            case 0:
            case 2:
                if (bullet.x > enemyTank.getX() && bullet.x < enemyTank.getX() + 40 &&
                        bullet.y > enemyTank.getY() && bullet.y < enemyTank.getY() + 60) {
                    //敌方坦克被击中
                    bullet.isAlive = false;
                    enemyTank.isAlive = false;
                    if (enemyTank instanceof EnemyTank) {
                        Recorder.addCount();
                    }
                    enemies.remove(enemyTank);
                    bombs.add(new Bomb(enemyTank.getX(),enemyTank.getY()));
                }
                break;
            case 1:
            case 3:
                if (bullet.x > enemyTank.getX() && bullet.x < enemyTank.getX() + 60 &&
                        bullet.y > enemyTank.getY() && bullet.y < enemyTank.getY() + 40) {
                    //敌方坦克被击中
                    bullet.isAlive = false;
                    enemyTank.isAlive = false;
                    if (enemyTank instanceof EnemyTank) {
                        Recorder.addCount();
                    }
                    enemies.remove(enemyTank);
                    bombs.add(new Bomb(enemyTank.getX(),enemyTank.getY()));
                }
                break;
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        //按键wasd控制坦克方向上左下右 并且改变坦克位置  坦克方向 0 向上 1-向有 2-向下 3-向左
        if (e.getKeyCode() == KeyEvent.VK_W) {
            mt.setDirection(0);
            //todo 边界问题
            mt.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            mt.setDirection(3);
            mt.moveLeft();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            mt.setDirection(2);
            mt.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            mt.setDirection(1);
            mt.moveRight();
            //mt.setBullet();
        }

        if (e.getKeyCode() == KeyEvent.VK_J) {
            //发射子弹

            System.out.println("开始射击");
            if (mt.bullets.size() < 6) {
                mt.shot();
            }

            //hitTank();

        }
        //重画tank
        repaint();

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            /*if (mt.getBullet()!=null&&mt.getBullet().isAlive) {
                for (com.xinfan.tank.EnemyTank enemy : enemies) {
                    hitTank(mt.getBullet(),enemy);

                }
            }*/

            if (mt.bullets != null) {
                /*for (int j = 0; j < enemies.size(); j++) {
                    for (int i = 0; i < mt.bullets.size(); i++) {
                        hitTank(mt.bullets.get(i),enemies.get(j));
                    }
                }*/
                for (int i = 0; i < mt.bullets.size(); i++) {
                    Bullet bullet = mt.bullets.get(i);
                    for (int i1 = 0; i1 < enemies.size(); i1++) {
                        hitTank(bullet,enemies.get(i1));
                    }
                }
            }

            hitMyTank();
            /*if (!mt.isAlive) {
                mt.setX(0);
                mt.setY(0);
            }*/

            this.repaint();
        }
    }
}
